package org.cklxh.waptime;

import org.cklxh.apn.ApnPojo;
import org.cklxh.apn.ApnServer;

import android.content.ContentResolver;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetWorkServer {


	/**
	 * 得到活动的Apn连接。
	 * 如果为null，表示为wifi。
	 * @return
	 * @throws Exception
	 */
	public ApnPojo getActiveNetworkApn(Context context)
		throws Exception{
		
		ApnPojo apnPojo;
		
		ConnectivityManager cwjManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo nwif=cwjManager.getActiveNetworkInfo();
		
		//判断是否有活动链接网络
		if (nwif != null && nwif.isConnected()) {
			//判断是否为WIFI
			if(nwif.getType()==ConnectivityManager.TYPE_WIFI){
				//Log.v("debug","WIFI链接");
				apnPojo=null;
			}else{
				//Log.v("debug","非WIFI链接");
				ApnServer apnServer = new ApnServer();
				ContentResolver contextResolver=context.getContentResolver();
				apnPojo=apnServer.getActiveApn(contextResolver);
			}
		}else{
			//Log.v("debug","无链接！");
			throw new RuntimeException("无网络连接，请连接到网络后重试！");
		}
		return apnPojo;

	}
	
	
}
